package cn.lixu.org.service.impl;

import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.basic.util.Md5Utils;
import cn.lixu.basic.util.StrUtils;
import cn.lixu.org.domain.Employee;
import cn.lixu.org.mapper.EmployeeMapper;
import cn.lixu.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //保存在emp表中
        employeeMapper.save(employee);
        //保存在关联表t_employee_role中
        employeeMapper.saveEmployeeRole(employee.getId(), employee.getRole_id());
    }

    @Override
    public void remove(Long eid) {
        // 先删除t_employee_role
        employeeMapper.removeEmployeeRole(eid);
        employeeMapper.remove(eid);

    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
        // 先删除t_employee_role
        employeeMapper.removeEmployeeRole(employee.getId());

        //保存在关联表t_employee_role中
        employeeMapper.saveEmployeeRole(employee.getId(), employee.getRole_id());


    }

    /**
     * 根据shopId查询所有的店铺员工
     * @param shopId
     * @return
     */
    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}

